/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import org.agilelang.generate.*;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.agilelang.object.ObjectClass;
import org.agilelang.object.ObjectClassAttribute;
import org.agilelang.object.ObjectFunction;
import org.agilelang.object.ObjectParam;
import org.agilelang.object.ObjectProject;
import org.agilelang.object.Read;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextArea;

/**
 *
 * @author Roderick
 */
public class AgileGenerator_f {

    public String name_file(String config, String name) {
        switch (config) {
            case "files":
                return Inflector.getInstance().pluralize(name).toLowerCase();
            case "FILES":
                return Inflector.getInstance().pluralize(name).toUpperCase();
            case "Files":
                return Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(name));
            case "file":
                return Inflector.getInstance().singularize(name).toLowerCase();
            case "FILE":
                return Inflector.getInstance().singularize(name).toUpperCase();
            case "File":
                return Inflector.getInstance().capitalize(Inflector.getInstance().singularize(name));
        }
        return "";
    }

    public String camel(String name) {
        return Inflector.getInstance().capitalize(name);
    }

    String path_tpl = "";
    String suf = "${";
    String posf = "}";
    String path_out = "";

    ObjectClass config = null;
    ObjectClass cls;
    Map<String, String> replace;

    public void process(String path_xml, String path_tpl, String path_out, List<ObjectClass> lists_config, JTextArea jtext) {
        ObjectProject agileproject = null;

        String functions = "";
        String attributes = "";
        this.path_tpl = path_tpl;
        this.path_out = path_out;

        Read rd = new Read();
        agileproject = rd.process(path_xml);

        //ReadConfig rc = new ReadConfig();
        //ConfigProject cp = rc.process(path_config);
        for (ObjectClass clsax : agileproject.getClasses()) {
            for (ObjectClass co : lists_config) {

                cls = clsax;
                config = null;
                if (co.getType().equals(cls.getType())) {
                    config = co;
                    //break;
                }

                if (config == null) {
                    continue;
                }

                String sqlCreate = "INSERT INTO //models//(//fileds_split_comma//) values (//fileds_values_split_comma//)";
                String sqlRead = "SELECT //fileds_split_comma// FROM //models// WHERE id = //id//";
                String sqlUpdate = "UPDATE  //models// //fileds_egual_value_split_comma// WHERE id = //id//";
                String sqlDelete = "DELETE FROM //models// WHERE id = //id//";
                String sqlSelect = "SELECT //fileds_split_comma// FROM //models//";
                String sqlValues = "//fileds_split_comma//";

                // Atributos de la clase   
                for (ObjectFunction fun : config.getFunctions()) {
                    if (fun.getName().equals("defineTemplate")) {
                        Map<String, Object> data = new HashMap<String, Object>();

                        String filename = "", ext = "", template = "", pathfolder = "", pathview = "";
                        for (ObjectParam attr : fun.getParams()) {
                            if (attr.getName().equals("filename")) {
                                filename = attr.getValue();
                            }
                            if (attr.getName().equals("ext")) {
                                ext = attr.getValue();
                            }
                            if (attr.getName().equals("template")) {
                                template = attr.getValue();
                            }
                            if (attr.getName().equals("pathfolder")) {
                                pathfolder = attr.getValue();
                            }
                            if (attr.getName().equals("pathview")) {
                                pathview = attr.getValue().replace("$name", cls.getName());
                                File tmp = new File(path_out + File.separator + pathview);
                                if (!tmp.exists()) {
                                    if (tmp.mkdir()) {
                                        System.out.println("Directorio temporal creado : " + path_out + File.separator + pathview);
                                    } else {
                                        System.err.println("Error no se puede crear directorio temporal");
                                    }
                                } else {
                                    System.out.println("Directorio temporal ya existe : " + path_out + File.separator + pathview);
                                }
                            }
                        }

                        Partial tpl_file = new Partial(path_tpl + File.separator + template);
                        for (ObjectClassAttribute attr : cls.getClassattributes()) {
                            if (attr.getName().equals("name")) {
                                tpl_file.replace(suf + "names" + posf, Inflector.getInstance().pluralize(cls.getName()).toLowerCase());
                                tpl_file.replace(suf + "NAMES" + posf, Inflector.getInstance().pluralize(cls.getName()).toUpperCase());
                                tpl_file.replace(suf + "Names" + posf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(cls.getName())));
                                tpl_file.replace(suf + "name" + posf, Inflector.getInstance().singularize(cls.getName()).toLowerCase());
                                tpl_file.replace(suf + "NAME" + posf, Inflector.getInstance().singularize(cls.getName()).toUpperCase());
                                tpl_file.replace(suf + "Name" + posf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(cls.getName())));

                                data.put("names", Inflector.getInstance().pluralize(cls.getName()).toLowerCase());
                                data.put("NAMES", Inflector.getInstance().pluralize(cls.getName()).toUpperCase());
                                data.put("Names", Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(cls.getName())));
                                data.put("name", Inflector.getInstance().singularize(cls.getName()).toLowerCase());
                                data.put("NAME", Inflector.getInstance().singularize(cls.getName()).toUpperCase());
                                data.put("Name", Inflector.getInstance().capitalize(Inflector.getInstance().singularize(cls.getName())));

                            } else {
                                if (attr.getName().endsWith("list")) {
                                    tpl_file.replace(suf + attr.getName() + posf, attr.getValue());
                                    List<String> data_list = new ArrayList<>();
                                    String[] data_array = attr.getValue().split(",");
                                    for (String val : data_array) {
                                        data_list.add(val);
                                    }
                                    data.put(attr.getName(), data_list);
                                } else {
                                    tpl_file.replace(suf + attr.getName() + posf, attr.getValue());
                                    data.put(attr.getName(), attr.getValue());
                                }

                                /*tpl_file.replace(suf + Inflector.getInstance().pluralize(attr.getName()).toLowerCase() + suf, Inflector.getInstance().pluralize(attr.getValue()).toLowerCase());
                                 tpl_file.replace(suf + Inflector.getInstance().pluralize(attr.getName()).toUpperCase() + suf, Inflector.getInstance().pluralize(attr.getValue()).toUpperCase());
                                 tpl_file.replace(suf + Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(attr.getName())) + suf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(attr.getValue())));
                                 tpl_file.replace(suf + Inflector.getInstance().singularize(attr.getName()).toLowerCase() + suf, Inflector.getInstance().singularize(attr.getValue()).toLowerCase());
                                 tpl_file.replace(suf + Inflector.getInstance().singularize(attr.getName()).toUpperCase() + suf, Inflector.getInstance().singularize(attr.getValue()).toUpperCase());
                                 tpl_file.replace(suf + Inflector.getInstance().capitalize(Inflector.getInstance().singularize(attr.getName())) + suf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(attr.getValue())));
                                 */
                            }
                        }

                        //--------------------------------------------------------------
                        // funciones
                        replace = new HashMap<>();
                        //------------------------------------------------------------------
                        //process_attribute();//config, cls, replace
                        //-------------------------------------------------------------
                        /*for (ObjectFunction fun1 : cls.getFunctions()) {
                         int index = 0;
                         if (fun1.getDefine("function_type").equals("addAttribute") && config.isFunctionAddAttrribute(fun1.getName())) {
                         Partial tpl_function = new Partial(path_tpl + File.separator + config.getFunctionAddAttrributeParam(fun1.getName(), "template"));
                         tpl_function.replace(suf + "index" + posf, String.valueOf(index));
                         index++;
                         for (ObjectParam attr : fun1.getParams()) {
                         if (attr.getName().equals("name")) {
                         tpl_function.replace(suf + "names" + posf, Inflector.getInstance().pluralize(attr.getValue()).toLowerCase());
                         tpl_function.replace(suf + "NAMES" + posf, Inflector.getInstance().pluralize(attr.getValue()).toUpperCase());
                         tpl_function.replace(suf + "Names" + posf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(attr.getValue())));
                         tpl_function.replace(suf + "name" + posf, Inflector.getInstance().singularize(attr.getValue()).toLowerCase());
                         tpl_function.replace(suf + "NAME" + posf, Inflector.getInstance().singularize(attr.getValue()).toUpperCase());
                         tpl_function.replace(suf + "Name" + posf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(attr.getValue())));
                         } else {

                         tpl_function.replace(suf + attr.getName() + posf, attr.getValue());
                         System.err.println(">>>>>>>>>>>>>>>>>>>>>>>>>>" + suf + attr.getName() + posf);
                         }
                         }
                         tpl_function.replace(suf + "clsnames" + posf, Inflector.getInstance().pluralize(cls.getName()).toLowerCase());
                         tpl_function.replace(suf + "CLSNAMES" + posf, Inflector.getInstance().pluralize(cls.getName()).toUpperCase());
                         tpl_function.replace(suf + "Clsnames" + posf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(cls.getName())));
                         tpl_function.replace(suf + "clsname" + posf, Inflector.getInstance().singularize(cls.getName()).toLowerCase());
                         tpl_function.replace(suf + "CLSNAME" + posf, Inflector.getInstance().singularize(cls.getName()).toUpperCase());
                         tpl_function.replace(suf + "Clsname" + posf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(cls.getName())));

                         if (replace.containsKey(config.getFunctionAddAttrributeParam(fun1.getName(), "replace"))) {
                         String value = replace.get(config.getFunctionAddAttrributeParam(fun1.getName(), "replace"));
                         value += tpl_function.getPtl();
                         replace.put(config.getFunctionAddAttrributeParam(fun1.getName(), "replace"), value);
                         } else {
                         String value = tpl_function.getPtl();
                         replace.put(config.getFunctionAddAttrributeParam(fun1.getName(), "replace"), value);
                         }
                         }
                         }*/
                        process_function(pathview);//config, cls, replace
                        //-------------------------------------------------------------------

                        String fileds_split_comma = "", fileds_values_split_comma = "";
                        String fileds_split_comma_read = "";
                        String fileds_split_comma_update = "";

                        for (ObjectFunction fun1 : cls.getFunctions()) {
                            if (fun1.getDefine("function_type").equals("addAttribute")
                                    && config.isFunctionAddAttrribute(fun1.getName())) {
                                for (ObjectParam attr : fun1.getParams()) {

                                    if (attr.getName().equals("name")) {

                                        fileds_split_comma += attr.getValue();
                                        fileds_values_split_comma += "?, ";
                                        fileds_split_comma += ", ";

                                        //----------------------------------------------
                                        fileds_split_comma_read += attr.getValue();
                                        fileds_split_comma_read += ", ";
                                        //----------------------------------------------
                                        fileds_split_comma_update += attr.getValue() + " = ?";
                                        fileds_split_comma_update += ", ";
                                    }
                                }
                            }
                        }

                        if (fileds_split_comma.length() > 0) {
                            sqlCreate = sqlCreate.replace("//models//", name_file("files", cls.getName()));
                            sqlCreate = sqlCreate.replace("//fileds_split_comma//", fileds_split_comma.substring(0, fileds_split_comma.length() - 2));
                            sqlCreate = sqlCreate.replace("//fileds_values_split_comma//", fileds_values_split_comma.substring(0, fileds_values_split_comma.length() - 2));
                            //----------------------------------------------------------
                            sqlRead = sqlRead.replace("//models//", name_file("files", cls.getName()));
                            sqlRead = sqlRead.replace("//fileds_split_comma//", fileds_split_comma_read.substring(0, fileds_split_comma_read.length() - 2));
                            sqlRead = sqlRead.replace("//id//", "?");
                            //----------------------------------------------------------
                            sqlUpdate = sqlUpdate.replace("//models//", name_file("files", cls.getName()));
                            sqlUpdate = sqlUpdate.replace("//fileds_egual_value_split_comma//", fileds_split_comma_update.substring(0, fileds_split_comma_update.length() - 2));
                            sqlUpdate = sqlUpdate.replace("//id//", "?");
                            //----------------------------------------------------------
                            sqlDelete = sqlDelete.replace("//models//", name_file("files", cls.getName()));
                            sqlDelete = sqlDelete.replace("//id//", "?");
                            //----------------------------------------------------------
                            sqlSelect = sqlSelect.replace("//models//", name_file("files", cls.getName()));
                            sqlSelect = sqlSelect.replace("//fileds_split_comma//", fileds_split_comma.substring(0, fileds_split_comma.length() - 2));
                            sqlSelect = sqlSelect.replace("//id//", "?");
                            //----------------------------------------------------------
                            sqlValues = sqlValues.replace("//models//", name_file("files", cls.getName()));
                            sqlValues = sqlValues.replace("//fileds_split_comma//", fileds_split_comma.substring(0, fileds_split_comma.length() - 2));
                            sqlValues = sqlValues.replace("//id//", "?");
                        } else {
                            sqlCreate = sqlCreate.replace("//models//", name_file("files", cls.getName()));
                            //sqlCreate = sqlCreate.replace("//fileds_split_comma//", fileds_split_comma.substring(0, fileds_split_comma.length() - 2));
                            //sqlCreate = sqlCreate.replace("//fileds_values_split_comma//", fileds_values_split_comma.substring(0, fileds_values_split_comma.length() - 2));
                            //----------------------------------------------------------
                            sqlRead = sqlRead.replace("//models//", name_file("files", cls.getName()));
                            //sqlRead = sqlRead.replace("//fileds_split_comma//", fileds_split_comma_read.substring(0, fileds_split_comma_read.length() - 2));
                            //sqlRead = sqlRead.replace("//id//", "?");
                            //----------------------------------------------------------
                            sqlUpdate = sqlUpdate.replace("//models//", name_file("files", cls.getName()));
                            //sqlUpdate = sqlUpdate.replace("//fileds_egual_value_split_comma//", fileds_split_comma_update.substring(0, fileds_split_comma_update.length() - 2));
                            //sqlUpdate = sqlUpdate.replace("//id//", "?");
                            //----------------------------------------------------------
                            sqlDelete = sqlDelete.replace("//models//", name_file("files", cls.getName()));
                            //sqlDelete = sqlDelete.replace("//id//", "?");
                            //----------------------------------------------------------
                            sqlSelect = sqlSelect.replace("//models//", name_file("files", cls.getName()));
                            //sqlSelect = sqlSelect.replace("//fileds_split_comma//", fileds_split_comma.substring(0, fileds_split_comma.length() - 2));
                            //sqlSelect = sqlSelect.replace("//id//", "?");
                            //----------------------------------------------------------
                            sqlValues = sqlValues.replace("//models//", name_file("files", cls.getName()));
                            //sqlValues = sqlValues.replace("//fileds_split_comma//", fileds_split_comma.substring(0, fileds_split_comma.length() - 2));
                            //sqlValues = sqlValues.replace("//id//", "?");

                        }
                        tpl_file.replace(suf + "sqlCreate" + posf, sqlCreate);
                        tpl_file.replace(suf + "sqlRead" + posf, sqlRead);
                        tpl_file.replace(suf + "sqlUpdate" + posf, sqlUpdate);
                        tpl_file.replace(suf + "sqlDelete" + posf, sqlDelete);
                        tpl_file.replace(suf + "sqlSelect" + posf, sqlSelect);
                        tpl_file.replace(suf + "sqlValues" + posf, sqlValues);
                        for (Map.Entry mapEntry : replace.entrySet()) {
                            tpl_file.replace(suf + mapEntry.getKey() + posf, mapEntry.getValue().toString());
                        }

                        /*UtilFile.writefile(name_file(config.getDefineTemplate("filename"), cls.getName()) + config.getDefineTemplate("ext"), path_out, tpl_file.getPtl());
                         jtext.append(path_out + File.separator + name_file(config.getDefineTemplate("filename"), cls.getName()) + config.getDefineTemplate("ext"));
                         jtext.append("\n");*/
                        UtilFile.writefile(name_file(filename, pathfolder + cls.getName()) + ext, path_out, tpl_file.getPtl());
                        jtext.append(path_out + File.separator + name_file(filename, cls.getName()) + ext);
                        jtext.append("\n");
                        ///-----------------------------------------------------

                        System.out.println("FREEMAKER ----" + path_tpl);

                        Configuration cfg = new Configuration();
                        try {
                            cfg.setDirectoryForTemplateLoading(new File(path_tpl));
                            Template template_f = cfg.getTemplate(template);
                            Writer out = new OutputStreamWriter(System.out);
                            template_f.process(data, out);
                            out.flush();
                            // File output
                            System.out.println(path_out + File.separator + name_file(filename, pathfolder + cls.getName()) + ext + "f");
                            Writer file = new FileWriter(new File(path_out + File.separator + name_file(filename, pathfolder + cls.getName()) + ext + "f"));
                            template_f.process(data, file);
                            file.flush();
                            file.close();
                        } catch (IOException ex) {
                            Logger.getLogger(AgileGenerator_f.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (TemplateException ex) {
                            Logger.getLogger(AgileGenerator_f.class.getName()).log(Level.SEVERE, null, ex);
                        }

                    }
                }
            }
        }
    }

    public void process_attribute() {//ObjectClass config, ObjectClass cls, Map<String, String> replace

    }

    public void process_function(String pathview) {//ObjectClass config, ObjectClass cls, Map<String, String> replace
        for (ObjectFunction con_fun : config.getFunctions()) {
            for (ObjectFunction fun1 : cls.getFunctions()) {

                if (con_fun.getDefine("type").equals(fun1.getName())) {
                    //if (con_fun.getDefine("function_type").equals("addFunction")) {
                    if (con_fun.getName().equals("addFunction") && fun1.getDefine("function_type").equals("addFunction")) {

                        Partial tpl_function = new Partial(path_tpl + File.separator + con_fun.getDefine("template"));
                        for (ObjectParam attr : fun1.getParams()) {
                            if (attr.getName().equals("name")) {
                                //tpl_function.replace(suf + "name_function" + suf, camel(attr.getValue()));
                                //tpl_function.replace(suf + attr.getName() + suf, attr.getValue());

                                tpl_function.replace(suf + "names" + posf, Inflector.getInstance().pluralize(attr.getValue()).toLowerCase());
                                tpl_function.replace(suf + "NAMES" + posf, Inflector.getInstance().pluralize(attr.getValue()).toUpperCase());
                                tpl_function.replace(suf + "Names" + posf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(attr.getValue())));
                                tpl_function.replace(suf + "name" + posf, Inflector.getInstance().singularize(attr.getValue()).toLowerCase());
                                tpl_function.replace(suf + "NAME" + posf, Inflector.getInstance().singularize(attr.getValue()).toUpperCase());
                                tpl_function.replace(suf + "Name" + posf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(attr.getValue())));

                            } else {

                                //tpl_function.replace(suf + attr.getName() + suf, attr.getValue());
                                tpl_function.replace(suf + Inflector.getInstance().pluralize(attr.getName()).toLowerCase() + posf, Inflector.getInstance().pluralize(attr.getValue()).toLowerCase());
                                tpl_function.replace(suf + Inflector.getInstance().pluralize(attr.getName()).toUpperCase() + posf, Inflector.getInstance().pluralize(attr.getValue()).toUpperCase());
                                tpl_function.replace(suf + Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(attr.getName())) + posf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(attr.getValue())));
                                tpl_function.replace(suf + Inflector.getInstance().singularize(attr.getName()).toLowerCase() + posf, Inflector.getInstance().singularize(attr.getValue()).toLowerCase());
                                tpl_function.replace(suf + Inflector.getInstance().singularize(attr.getName()).toUpperCase() + posf, Inflector.getInstance().singularize(attr.getValue()).toUpperCase());
                                tpl_function.replace(suf + Inflector.getInstance().capitalize(Inflector.getInstance().singularize(attr.getName())) + posf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(attr.getValue())));

                            }
                        }
                        tpl_function.replace(suf + "clsnames" + posf, Inflector.getInstance().pluralize(cls.getName()).toLowerCase());
                        tpl_function.replace(suf + "CLSNAMES" + posf, Inflector.getInstance().pluralize(cls.getName()).toUpperCase());
                        tpl_function.replace(suf + "Clsnames" + posf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(cls.getName())));
                        tpl_function.replace(suf + "clsname" + posf, Inflector.getInstance().singularize(cls.getName()).toLowerCase());
                        tpl_function.replace(suf + "CLSNAME" + posf, Inflector.getInstance().singularize(cls.getName()).toUpperCase());
                        tpl_function.replace(suf + "Clsname" + posf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(cls.getName())));

                        //functions += tpl_function.getPtl();
                        if (replace.containsKey(con_fun.getDefine("replace"))) {
                            String value = replace.get(con_fun.getDefine("replace"));
                            value += tpl_function.getPtl();
                            replace.put(con_fun.getDefine("replace"), value);
                        } else {
                            String value = tpl_function.getPtl();
                            replace.put(con_fun.getDefine("replace"), value);
                        }
                    }

                    if (con_fun.getName().equals("addView") && fun1.getDefine("function_type").equals("addView")) {
                        String str_file = "";
                        Map<String, Object> data = new HashMap<String, Object>();
                        Partial tpl_function = new Partial(path_tpl + File.separator + con_fun.getDefine("template"));
                        for (ObjectParam attr : fun1.getParams()) {

                            if (attr.getName().equals("name")) {
                                str_file = attr.getValue();
                                data.put("names", Inflector.getInstance().pluralize(cls.getName()).toLowerCase());
                                data.put("NAMES", Inflector.getInstance().pluralize(cls.getName()).toUpperCase());
                                data.put("Names", Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(cls.getName())));
                                data.put("name", Inflector.getInstance().singularize(cls.getName()).toLowerCase());
                                data.put("NAME", Inflector.getInstance().singularize(cls.getName()).toUpperCase());
                                data.put("Name", Inflector.getInstance().capitalize(Inflector.getInstance().singularize(cls.getName())));
                                tpl_function.replace(suf + attr.getName() + posf, attr.getValue());
                            } else {
                                if (attr.getName().endsWith("list")) {
                                    tpl_function.replace(suf + attr.getName() + posf, attr.getValue());
                                    List<String> data_list = new ArrayList<>();
                                    String[] data_array = attr.getValue().split(",");
                                    for (String val : data_array) {
                                        data_list.add(val);
                                    }
                                    data.put(attr.getName(), data_list);
                                } else {
                                    tpl_function.replace(suf + attr.getName() + posf, attr.getValue());
                                    data.put(attr.getName(), attr.getValue());
                                }

                            }
                        }
                        tpl_function.replace(suf + "clsnames" + posf, Inflector.getInstance().pluralize(cls.getName()).toLowerCase());
                        tpl_function.replace(suf + "CLSNAMES" + posf, Inflector.getInstance().pluralize(cls.getName()).toUpperCase());
                        tpl_function.replace(suf + "Clsnames" + posf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(cls.getName())));
                        tpl_function.replace(suf + "clsname" + posf, Inflector.getInstance().singularize(cls.getName()).toLowerCase());
                        tpl_function.replace(suf + "CLSNAME" + posf, Inflector.getInstance().singularize(cls.getName()).toUpperCase());
                        tpl_function.replace(suf + "Clsname" + posf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(cls.getName())));

                        data.put("clsnames", Inflector.getInstance().pluralize(cls.getName()).toLowerCase());
                        data.put("CLSNAMES", Inflector.getInstance().pluralize(cls.getName()).toUpperCase());
                        data.put("Clsnames", Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(cls.getName())));
                        data.put("clsname", Inflector.getInstance().singularize(cls.getName()).toLowerCase());
                        data.put("CLSNAME", Inflector.getInstance().singularize(cls.getName()).toUpperCase());
                        data.put("Clsname", Inflector.getInstance().capitalize(Inflector.getInstance().singularize(cls.getName())));

                        /*for (Map.Entry mapEntry : replace.entrySet()) {
                            tpl_function.replace(suf + mapEntry.getKey() + posf, mapEntry.getValue().toString());
                        }*/
                        UtilFile.writefile(pathview + con_fun.getDefine("pathfolder") + str_file + con_fun.getDefine("ext"), path_out, tpl_function.getPtl());
                        System.out.println("FREEMAKER ----" + path_tpl);

                        Configuration cfg = new Configuration();
                        try {
                            cfg.setDirectoryForTemplateLoading(new File(path_tpl));
                            Template template_f = cfg.getTemplate(con_fun.getDefine("template"));
                            Writer out = new OutputStreamWriter(System.out);
                            template_f.process(data, out);
                            out.flush();
                            // File output
                            System.out.println(path_out + File.separator + pathview + con_fun.getDefine("pathfolder") + str_file + con_fun.getDefine("ext") + "fv");
                            Writer file = new FileWriter(new File(path_out + File.separator +pathview + con_fun.getDefine("pathfolder") + str_file + con_fun.getDefine("ext") + "fv"));
                            template_f.process(data, file);
                            file.flush();
                            file.close();
                        } catch (IOException ex) {
                            Logger.getLogger(AgileGenerator_f.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (TemplateException ex) {
                            Logger.getLogger(AgileGenerator_f.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
        }
    }

    public static void main(String ar[]) {
        String path_xml = "E:\\agile\\AgileLang\\Test\\out.xml";
        String path_tpl = "E:\\agile\\AgileLang\\Test\\tmpl_java";
        String path_out = "E:\\agile\\AgileLang\\Test\\out";
        String path_config = "E:\\agile\\AgileLang\\Test\\config_java.xml";
        AgileGenerator_f ag = new AgileGenerator_f();
        //ag.process(path_xml, path_tpl, path_out, path_config, new JTextArea());
    }

}
